package com.sri.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.sri.business.user.UserManager;
import com.sri.cache.CacheManager;
import com.sri.formBeans.NewUserForm;

public class UserAction extends DispatchAction {
	Logger logger = LogManager.getLogger("UserAction");

	public ActionForward addUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("addUser");
		request.setAttribute("stateCities", CacheManager.getCitiesByStateId(2));
		if (UserManager.addUser((NewUserForm) form))
			return mapping.findForward("success");
		else
			return mapping.findForward("failure");
	}

	public ActionForward newUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("addUser");
		request.setAttribute("stateCities", CacheManager.getCitiesByStateId(1));
		NewUserForm userForm = (NewUserForm) form;
		userForm.setGender(true);
		return mapping.findForward("newUser");
	}

	/**
	 * Method getCities by stateId
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getCities(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int stateId = Integer.parseInt(request.getParameter("stateId"));
		request.setAttribute("stateCities", CacheManager
				.getCitiesByStateId(stateId));
		return mapping.findForward("newUser");
	}

}
